﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TemplateWizard;
using System.IO;
using System.IO.Compression;

namespace SecurableWebFormWizard
{
    public class CopyFolderWizard : IWizard
    {
        string solutionPath = string.Empty;
        string projectPath = string.Empty;

        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {

        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {

        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {

        }

        public void RunFinished()
        {

        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            EnvDTE.DTE dte = automationObject as EnvDTE.DTE;

            var project = dte.Solution.Projects.Item(1);
            string pp = project.FullName;

            bool m_add = true;
            foreach (EnvDTE.ProjectItem item in project.ProjectItems)
            {
                if (item.Name.Equals("Scripts", StringComparison.InvariantCultureIgnoreCase))
                {
                    m_add = false;
                    break;
                }
            }

            if (m_add)
            {
                EnvDTE.ProjectItem m_scriptsFolder = project.ProjectItems.AddFolder("Scripts");

                EnvDTE.ProjectItem m_scriptsADCF = m_scriptsFolder.ProjectItems.AddFolder("yadcf");

                string m_item1Name = string.Format(@"{0}\{1}\jquery.dataTables.yadcf.css", m_scriptsFolder.Name, m_scriptsADCF.Name);
                
                string m_templateName = Path.GetFileName(customParams[0].ToString());

                string m_templateName1 = m_templateName.Replace("\\GridPageTemplate.vstemplate", "");

                //string zipfile = m_templateName;
                //using (ZipArchive zip = ZipFile.Open(zipfile, ZipArchiveMode.Read))
                //    foreach (ZipArchiveEntry entry in zip.Entries)
                //        if (entry.Name == "jquery.dataTables.yadcf.css")
                //            entry.ExtractToFile(m_item1Name);

                EnvDTE.ProjectItem m_item1 = project.ProjectItems.AddFromTemplate(m_templateName, "jquery.dataTables.yadcf.css");
            }
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            if (filePath.Contains("jquery.dataTables.yadcf.css"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
